Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Datatypes

Python Set

Sets: Unordered Collections of Unique Elements

Python sets are a fundamental data structure used to store collections of unique elements. They're designed to efficiently support mathematical set operations like membership testing, unions, intersections, and more.

Key Characteristics:

Unordered: Elements in a set have no specific order. You cannot access them by index, and the order can change between executions. The focus is on the presence of elements, not their sequence. ✦ Unique: Each element in a set must be unique. Duplicates are automatically removed. ✦ Mutable: You can add or remove elements from a set after its creation. ✦ Hashable: The members of a set must be hashable (i.e., of an immutable type like numbers, strings, or tuples).

Creating Sets

You can create sets in two primary ways: Using curly braces {}: Enclose elements in curly braces, separated by commas. Remember, empty curly braces {} create an empty dictionary, not a set.
Creating set in python using curly braces {} example cities = {"London", "Paris", "New York", "London"} # Duplicate "London" is removed vowels = {'a', 'e', 'i', 'o', 'u'} print(cities) print(vowels)

Output

{'New York', 'London', 'Paris'} {'u', 'i', 'o', 'e', 'a'}
Using the set() constructor: You can convert sequences like lists or tuples into sets. This is useful if you want to remove duplicates from an existing sequence.
Creating set with set() constructor in python basic example numbers = [1, 2, 3, 2, 4] unique_numbers = set(numbers) print(unique_numbers)

Output

{1, 2, 3, 4}

Operations on Sets

Membership Testing (in): Check if an element exists in a set. ✦ Adding an Element (add()): Insert a single element into the set. ✦ Removing an Element (remove()): Remove a specific element. Raises a KeyError if the element is not found. ✦ Union (|): Creates a new set containing all elements from both sets. ✦ Intersection (&): Creates a new set containing only elements present in both sets. ✦ Difference (-): Creates a new set containing elements in the first set but not in the second. ✦ Symmetric Difference: Symmetric Difference of A and B is a set of elements in A and B but not in both (excluding the intersection). Example
Example for multiple operations on set datatype in python cities = {"London", "Paris", "New York", "London","Delhi","Mumbai"} #Membership Testing (in) if "Paris" in cities: print("Paris is in the set.") #Adding new element cities.add("Tokyo") print("\n added new city\n",cities) #Removing an element cities.remove("London") print("\n Removed city\n",cities) #union european_cities = {"Berlin", "Rome","paris"} all_cities= european_cities | cities print("\n All cities : \n",all_cities) #intersection asian_cities = {"Tokyo","Beijing","Delhi"} common_cities = cities & asian_cities & all_cities print("\n common cities : \n",common_cities) #Difference non_european_cities = cities - european_cities print("\n non european cities : \n",non_european_cities) #Symmetric Difference print("\n symmetric diff :\n",european_cities ^ all_cities)

Output

Paris is in the set. added new city {'Mumbai', 'Paris', 'Tokyo', 'Delhi', 'New York', 'London'} Removed city {'Mumbai', 'Paris', 'Tokyo', 'Delhi', 'New York'} All cities : {'Rome', 'Berlin', 'Delhi', 'Paris', 'Tokyo', 'paris', 'Mumbai', 'New York'} common cities : {'Delhi', 'Tokyo'} non european cities : {'Delhi', 'Paris', 'Tokyo', 'Mumbai', 'New York'} symmetric diff : {'Delhi', 'Paris', 'Tokyo', 'Mumbai', 'New York'}

Python set methods

add() - Adds an element to the set ✦ clear() - Removes all the elements from the set ✦ copy() - Returns a copy of the set ✦ difference() - Returns a set containing the difference between two or more sets ✦ difference_update() - Removes the items in this set that are also included in another, specified set ✦ discard() - Remove the specified item ✦ intersection() - Returns a set, that is the intersection of two or more sets ✦ intersection_update() - Removes the items in this set that are not present in other, specified set(s) ✦ isdisjoint() - Returns whether two sets have a intersection or not ✦ issubset() - Returns whether another set contains this set or not ✦ issuperset() - Returns whether this set contains another set or not ✦ pop() - Removes an element from the set ✦ remove() - Removes the specified element ✦ symmetric_difference() - Returns a set with the symmetric differences of two sets ✦ symmetric_difference_update() - inserts the symmetric differences from this set and another ✦ union() - Return a set containing the union of sets ✦ update() - Update the set with another set, or any other iterable

More Points

Removing Duplicates: Quickly remove duplicates from lists or sequences, converting them into sets. ✦ Membership Testing: Efficiently check if an element exists within a large collection. ✦ Set Operations: Implement real-world problems involving combinations of sets.

  📌TAGS

★python ★ datatypes ★ set

Tutorials